import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Algo extends Setup {
	static PrintWriter pw;

	public static void main(String[] args) {
		FILE_PATH_USER_B = "../track1/validationIdx1.txt" + ".ser";
		setup();
		makePw();

		long time;

		time = System.currentTimeMillis();
		loop();
		pw.close();
		System.out.println(System.currentTimeMillis() - time);
	}

	static void makePw() {
		String dat = "val";
		if (FILE_PATH_USER_B.contains("tes")) {
			dat = "tes";
		}

		try {
			pw = new PrintWriter("alg" + dat + ".csv");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	static void loop() {
		printHeader();

		for (int bUserID = 0; bUserID < USERB.length; bUserID++) {
			int v = 0;
			for (int bItemID : USERB[bUserID].items) {
				print(bUserID);
				print(bItemID);

				predUser(bUserID);
				predItem(bItemID);

				print(v);
				print(USERB[bUserID].votes[v]);
				println();

				v++;
			}
		}
	}

	private static void printHeader() {
		print("bUserID");
		print("bItemID");

		print("aU.ilen");
		print("aUSum");
		print("aUMean");
		phist("aU");

		print("aI.ulen");
		print("aISum");
		print("aIMean");
		phist("aI");

		print("vnum");
		print("label");
		println();
	}

	static void phist(String s) {
		print(s + "b0");
		print(s + "b30");
		print(s + "b50");
		print(s + "b70");
		print(s + "b80");
		print(s + "b90");
		print(s + "b100");
	}

	static void predUser(int bUserID) {
		User aUser = USERA[bUserID];
		print(aUser.ilen);

		int aUserSum = 0;
		for (byte vote : aUser.votes) {
			aUserSum += vote;
		}
		print(aUserSum);
		print((int) (aUserSum / (double) aUser.ilen * 100));

		hist(aUser.votes);
	}

	static void predItem(int bItemID) {
		Item aItem = ITEMA[bItemID];
		print(aItem.ulen);

		int aItemSum = 0;
		for (byte vote : aItem.votes) {
			aItemSum += vote;
		}
		print(aItemSum);
		print((int) (aItemSum / (double) aItem.ulen * 100));

		hist(aItem.votes);
	}

	static void hist(byte[] votes) {
		int b0 = 0;
		int b30 = 0;
		int b50 = 0;
		int b70 = 0;
		int b80 = 0;
		int b90 = 0;
		int b100 = 0;

		for (byte vote : votes) {
			if (vote < 15)
				b0++;
			else if (vote < 45)
				b30++;
			else if (vote < 60)
				b50++;
			else if (vote < 75)
				b70++;
			else if (vote < 85)
				b80++;
			else if (vote < 95)
				b90++;
			else
				b100++;
		}

		print(b0);
		print(b30);
		print(b50);
		print(b70);
		print(b80);
		print(b90);
		print(b100);
	}

	private static void println() {
//		System.out.println();
		pw.println();
	}

	private static void print(String s) {
//		System.out.print(s + "\t");
		pw.print(s + "\t");
	}

	private static void print(int i) {
//		System.out.print(i);
//		System.out.print("\t");
		pw.print(i);
		pw.print("\t");

	}

}
